<!DOCTYPE html>
<html lang=en>
<head>
  <meta charset="utf-8">
  
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, minimal-ui">
  <meta name="renderer" content="webkit">
  <meta http-equiv="Cache-Control" content="no-transform" />
  <meta http-equiv="Cache-Control" content="no-siteapp" />
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta name="format-detection" content="telephone=no,email=no,adress=no">
  <!-- Color theme for statusbar -->
  <meta name="theme-color" content="#000000" />
  <!-- 强制页面在当前窗口以独立页面显示,防止别人在框架里调用页面 -->
  <meta http-equiv="window-target" content="_top" />
  
  
  <title>Hexo</title>
  <meta name="description" content="单任务1234567891011void setup() &amp;#123;  &#x2F;&#x2F; put your setup code here, to run once:  pinMode(10,OUTPUT);&amp;#125;void loop() &amp;#123;  &#x2F;&#x2F; put your main code here, to run repeatedly:  digitalWrite(10,!digitalRea">
<meta property="og:type" content="article">
<meta property="og:title" content="Hexo">
<meta property="og:url" content="https://gitee.com/yin-qiyu/myblog/2023/04/04/FreeRTOS/lonely_binary/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="单任务1234567891011void setup() &amp;#123;  &#x2F;&#x2F; put your setup code here, to run once:  pinMode(10,OUTPUT);&amp;#125;void loop() &amp;#123;  &#x2F;&#x2F; put your main code here, to run repeatedly:  digitalWrite(10,!digitalRea">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2023-04-04T06:56:35.461Z">
<meta property="article:modified_time" content="2023-04-28T16:04:01.687Z">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary">
  <!-- Canonical links -->
  <link rel="canonical" href="https://gitee.com/yin-qiyu/myblog/2023/04/04/FreeRTOS/lonely_binary/index.html">
  
    <link rel="alternate" href="/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png" type="image/x-icon">
  
  
<link rel="stylesheet" href="/yin-qiyu/myblog/css/style.css">

  
  
  
  
<meta name="generator" content="Hexo 6.3.0"></head>


<body class="main-center" itemscope itemtype="http://schema.org/WebPage">
  <header class="header" itemscope itemtype="http://schema.org/WPHeader">
  <div class="slimContent">
    <div class="navbar-header">
      
      
      <div class="profile-block text-center">
        <a id="avatar" href="https://github.com/cofess" target="_blank">
          <img class="img-circle img-rotate" src="/yin-qiyu/myblog/images/avatar.jpg" width="200" height="200">
        </a>
        <h2 id="name" class="hidden-xs hidden-sm">昵称</h2>
        <h3 id="title" class="hidden-xs hidden-sm hidden-md">Web Developer &amp; Designer</h3>
        <small id="location" class="text-muted hidden-xs hidden-sm"><i class="icon icon-map-marker"></i> Shenzhen, China</small>
      </div>
      
      <div class="search" id="search-form-wrap">

    <form class="search-form sidebar-form">
        <div class="input-group">
            <input type="text" class="search-form-input form-control" placeholder="Search" />
            <span class="input-group-btn">
                <button type="submit" class="search-form-submit btn btn-flat" onclick="return false;"><i class="icon icon-search"></i></button>
            </span>
        </div>
    </form>
    <div class="ins-search">
  <div class="ins-search-mask"></div>
  <div class="ins-search-container">
    <div class="ins-input-wrapper">
      <input type="text" class="ins-search-input" placeholder="Type something..." x-webkit-speech />
      <button type="button" class="close ins-close ins-selectable" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
    </div>
    <div class="ins-section-wrapper">
      <div class="ins-section-container"></div>
    </div>
  </div>
</div>


</div>
      <button class="navbar-toggle collapsed" type="button" data-toggle="collapse" data-target="#main-navbar" aria-controls="main-navbar" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
    </div>
    <nav id="main-navbar" class="collapse navbar-collapse" itemscope itemtype="http://schema.org/SiteNavigationElement" role="navigation">
      <ul class="nav navbar-nav main-nav ">
        
        
        <li class="menu-item menu-item-home">
          <a href="/yin-qiyu/myblog/.">
            
            <i class="icon icon-home-fill"></i>
            
            <span class="menu-title">Home</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-archives">
          <a href="/yin-qiyu/myblog/archives">
            
            <i class="icon icon-archives-fill"></i>
            
            <span class="menu-title">Archives</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-categories">
          <a href="/yin-qiyu/myblog/categories">
            
            <i class="icon icon-folder"></i>
            
            <span class="menu-title">Categories</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-tags">
          <a href="/yin-qiyu/myblog/tags">
            
            <i class="icon icon-tags"></i>
            
            <span class="menu-title">Tags</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-repository">
          <a href="/yin-qiyu/myblog/repository">
            
            <i class="icon icon-project"></i>
            
            <span class="menu-title">Repository</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-books">
          <a href="/yin-qiyu/myblog/books">
            
            <i class="icon icon-book-fill"></i>
            
            <span class="menu-title">Books</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-links">
          <a href="/yin-qiyu/myblog/links">
            
            <i class="icon icon-friendship"></i>
            
            <span class="menu-title">Links</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-about">
          <a href="/yin-qiyu/myblog/about">
            
            <i class="icon icon-cup-fill"></i>
            
            <span class="menu-title">About</span>
          </a>
        </li>
        
      </ul>
      
	
    <ul class="social-links">
    	
        <li><a href="https://github.com/cofess" target="_blank" title="Github" data-toggle=tooltip data-placement=top><i class="icon icon-github"></i></a></li>
        
        <li><a href="http://weibo.com/cofess" target="_blank" title="Weibo" data-toggle=tooltip data-placement=top><i class="icon icon-weibo"></i></a></li>
        
        <li><a href="https://twitter.com/iwebued" target="_blank" title="Twitter" data-toggle=tooltip data-placement=top><i class="icon icon-twitter"></i></a></li>
        
        <li><a href="https://www.behance.net/cofess" target="_blank" title="Behance" data-toggle=tooltip data-placement=top><i class="icon icon-behance"></i></a></li>
        
        <li><a href="/yin-qiyu/myblog/atom.xml" target="_blank" title="Rss" data-toggle=tooltip data-placement=top><i class="icon icon-rss"></i></a></li>
        
    </ul>

    </nav>
  </div>
</header>

  
    <aside class="sidebar" itemscope itemtype="http://schema.org/WPSideBar">
  <div class="slimContent">
    
      <div class="widget">
    <h3 class="widget-title">Board</h3>
    <div class="widget-body">
        <div id="board">
            <div class="content">
                <p>欢迎交流与分享经验!</p>
            </div>
        </div>
    </div>
</div>

    
      

    
      

    
      
    
      
  <div class="widget">
    <h3 class="widget-title">Archive</h3>
    <div class="widget-body">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/yin-qiyu/myblog/archives/2023/07/">July 2023</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/yin-qiyu/myblog/archives/2023/04/">April 2023</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/yin-qiyu/myblog/archives/2023/03/">March 2023</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/yin-qiyu/myblog/archives/2023/02/">February 2023</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/yin-qiyu/myblog/archives/2023/01/">January 2023</a><span class="archive-list-count">5</span></li><li class="archive-list-item"><a class="archive-list-link" href="/yin-qiyu/myblog/archives/2022/04/">April 2022</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/yin-qiyu/myblog/archives/2022/03/">March 2022</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/yin-qiyu/myblog/archives/2022/02/">February 2022</a><span class="archive-list-count">7</span></li></ul>
    </div>
  </div>


    
      
  <div class="widget">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget-body">
      <ul class="recent-post-list list-unstyled no-thumbnail">
        
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                
              </p>
              <p class="item-title">
                <a href="/yin-qiyu/myblog/2023/07/13/hello-world/" class="title">Hello World</a>
              </p>
              <p class="item-date">
                <time datetime="2023-07-13T14:49:10.957Z" itemprop="datePublished">2023-07-13</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                
              </p>
              <p class="item-title">
                <a href="/yin-qiyu/myblog/2023/04/04/FreeRTOS/lonely_binary/" class="title">(no title)</a>
              </p>
              <p class="item-date">
                <time datetime="2023-04-04T06:56:35.461Z" itemprop="datePublished">2023-04-04</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                
              </p>
              <p class="item-title">
                <a href="/yin-qiyu/myblog/2023/03/16/esp/esp-idf/issue/" class="title">(no title)</a>
              </p>
              <p class="item-date">
                <time datetime="2023-03-16T06:02:58.279Z" itemprop="datePublished">2023-03-16</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                
              </p>
              <p class="item-title">
                <a href="/yin-qiyu/myblog/2023/02/26/%E5%A2%A8%E6%B0%B4%E5%B1%8F/Untitled/" class="title">(no title)</a>
              </p>
              <p class="item-date">
                <time datetime="2023-02-25T17:10:25.099Z" itemprop="datePublished">2023-02-26</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                
              </p>
              <p class="item-title">
                <a href="/yin-qiyu/myblog/2023/02/02/%E5%A2%A8%E6%B0%B4%E5%B1%8F/epaper/" class="title">(no title)</a>
              </p>
              <p class="item-date">
                <time datetime="2023-02-02T05:54:50.611Z" itemprop="datePublished">2023-02-02</time>
              </p>
            </div>
          </li>
          
      </ul>
    </div>
  </div>
  

    
  </div>
</aside>

  
  
<main class="main" role="main">
  <div class="content">
  <article id="post-FreeRTOS/lonely_binary" class="article article-type-post" itemscope itemtype="http://schema.org/BlogPosting">
    
    <div class="article-header">
      
      <div class="article-meta">
        <span class="article-date">
    <i class="icon icon-calendar-check"></i>
	<a href="/yin-qiyu/myblog/2023/04/04/FreeRTOS/lonely_binary/" class="article-date">
	  <time datetime="2023-04-04T06:56:35.461Z" itemprop="datePublished">2023-04-04</time>
	</a>
</span>
        
        

        

        <span class="post-comment"><i class="icon icon-comment"></i> <a href="/yin-qiyu/myblog/2023/04/04/FreeRTOS/lonely_binary/#comments" class="article-comment-link">Comments</a></span>
        
      </div>
    </div>
    <div class="article-entry marked-body" itemprop="articleBody">
      
        <h1 id="单任务"><a href="#单任务" class="headerlink" title="单任务"></a>单任务</h1><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">setup</span><span class="params">()</span> &#123;</span><br><span class="line">  <span class="comment">// put your setup code here, to run once:</span></span><br><span class="line">  pinMode(<span class="number">10</span>,OUTPUT);</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">loop</span><span class="params">()</span> &#123;</span><br><span class="line">  <span class="comment">// put your main code here, to run repeatedly:</span></span><br><span class="line">  digitalWrite(<span class="number">10</span>,!digitalRead(<span class="number">10</span>));</span><br><span class="line">  delay(<span class="number">1000</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="任务传单参数"><a href="#任务传单参数" class="headerlink" title="任务传单参数"></a>任务传单参数</h1><blockquote>
<p><code>byte *pbLEDPIN</code> 是一个指向 <code>byte</code> 类型的指针变量，而 <code>byte LEDPIN = *(byte *)pt</code> 是将 <code>void</code> 指针转换为 <code>byte</code> 类型，并将其值赋给 <code>LEDPIN</code> 变量。在这个特定的代码中，<code>byte *pbLEDPIN</code> 没有使用，而是使用了 <code>void *pt</code> 作为传递参数的方法。</p>
<p>注意到在 <code>task1</code> 中，使用硬编码方式来控制 LED 的引脚，而在 <code>task2</code> 中，通过传递一个指向 <code>byte</code> 变量的 <code>void</code> 指针参数来控制 LED 的引脚。</p>
</blockquote>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">  程序： FREERTOS - 单个参数传递</span></span><br><span class="line"><span class="comment">        大家在看本程序的时候，需要对指针非常的了解</span></span><br><span class="line"><span class="comment">        知道 * -&gt; &amp;的作用</span></span><br><span class="line"><span class="comment">  作业： 添加LED3_PIN 15</span></span><br><span class="line"><span class="comment">  公众号：孤独的二进制</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line">byte LED1_PIN = <span class="number">23</span>;</span><br><span class="line">byte LED2_PIN = <span class="number">21</span>;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">task1</span><span class="params">(<span class="type">void</span> *pt)</span> &#123;</span><br><span class="line">  byte * pbLEDPIN;</span><br><span class="line">  pbLEDPIN = (byte *)pt;</span><br><span class="line"></span><br><span class="line">  byte LEDPIN;</span><br><span class="line">  LEDPIN = *pbLEDPIN;</span><br><span class="line">  pinMode(LEDPIN, OUTPUT);</span><br><span class="line">  <span class="keyword">while</span> (<span class="number">1</span>) &#123;</span><br><span class="line">    digitalWrite(LEDPIN, !digitalRead(LEDPIN));</span><br><span class="line">    vTaskDelay(<span class="number">1000</span>);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">task2</span><span class="params">(<span class="type">void</span> *pt)</span> &#123;</span><br><span class="line">  byte LEDPIN = *(byte *)pt;</span><br><span class="line">  pinMode(LEDPIN, OUTPUT);</span><br><span class="line">  <span class="keyword">while</span> (<span class="number">1</span>) &#123;</span><br><span class="line">    digitalWrite(LEDPIN, !digitalRead(LEDPIN));</span><br><span class="line">    vTaskDelay(<span class="number">3000</span>);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">setup</span><span class="params">()</span> &#123;</span><br><span class="line">  Serial.begin(<span class="number">9600</span>);</span><br><span class="line"></span><br><span class="line">  byte * pbLED1PIN;</span><br><span class="line">  pbLED1PIN = &amp;LED1_PIN;</span><br><span class="line"></span><br><span class="line">  <span class="type">void</span> * pvLED1PIN;</span><br><span class="line">  pvLED1PIN = (<span class="type">void</span> *)pbLED1PIN;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">if</span> (xTaskCreate(task1,</span><br><span class="line">                  <span class="string">&quot;Blink 23&quot;</span>,</span><br><span class="line">                  <span class="number">1024</span>,</span><br><span class="line">                  pvLED1PIN,</span><br><span class="line">                  <span class="number">1</span>,</span><br><span class="line">                  <span class="literal">NULL</span>) == pdPASS)</span><br><span class="line">    Serial.println(<span class="string">&quot;Task1 Created.&quot;</span>);</span><br><span class="line"></span><br><span class="line">  <span class="keyword">if</span> (xTaskCreate(task2,</span><br><span class="line">                  <span class="string">&quot;Blink 21&quot;</span>,</span><br><span class="line">                  <span class="number">1024</span>,</span><br><span class="line">                  (<span class="type">void</span> *)&amp;LED2_PIN,</span><br><span class="line">                  <span class="number">1</span>,</span><br><span class="line">                  <span class="literal">NULL</span>) == pdPASS)</span><br><span class="line">    Serial.println(<span class="string">&quot;Task2 Created.&quot;</span>);</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">loop</span><span class="params">()</span> &#123;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>





<h1 id="传多参"><a href="#传多参" class="headerlink" title="传多参"></a>传多参</h1><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">定义了一个名为LEDFLASH的结构体，其中包含两个成员变量：</span></span><br><span class="line"><span class="comment">一个是byte类型的引脚号（pin），另一个是int类型的LED闪烁的时间间隔（delayTime）。</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span> &#123;</span></span><br><span class="line">  byte pin;</span><br><span class="line">  <span class="type">int</span> delayTime;</span><br><span class="line">&#125; LEDFLASH;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">void ledFlash(void *pt) &#123;</span></span><br><span class="line"><span class="comment">  LEDFLASH * ptLedFlash = (LEDFLASH *)pt;</span></span><br><span class="line"><span class="comment">  LEDFLASH led = *ptLedFlash;</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">  pinMode(led.pin,OUTPUT);</span></span><br><span class="line"><span class="comment">  while (1) &#123;</span></span><br><span class="line"><span class="comment">    digitalWrite(led.pin, !digitalRead(led.pin));</span></span><br><span class="line"><span class="comment">    vTaskDelay(led.delayTime);</span></span><br><span class="line"><span class="comment">  &#125;</span></span><br><span class="line"><span class="comment">&#125;</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">定义了一个名为ledFlash的函数，该函数接收一个指针类型的参数pt，指向一个LEDFLASH类型的结构体变量。</span></span><br><span class="line"><span class="comment">在函数内部，首先将指针pt转换成一个LEDFLASH类型的指针ptLedFlash，然后使用箭头操作符-&gt;访问结构体变量中的成员变量，将引脚号和延迟时间分别赋值给局部变量pin和delayTime。</span></span><br><span class="line"><span class="comment">接下来，将LED的引脚设置为输出模式，并使用while循环不断地反转LED的状态。在每次反转后，通过vTaskDelay函数延迟指定的时间间隔。</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">ledFlash</span><span class="params">(<span class="type">void</span> *pt)</span> &#123;</span><br><span class="line">  LEDFLASH * ptLedFlash = (LEDFLASH *)pt;</span><br><span class="line">  byte pin = ptLedFlash-&gt;pin;</span><br><span class="line">  <span class="type">int</span> delayTime = ptLedFlash-&gt;delayTime;</span><br><span class="line"></span><br><span class="line">  pinMode(pin,OUTPUT);</span><br><span class="line">  <span class="keyword">while</span> (<span class="number">1</span>) &#123;</span><br><span class="line">    digitalWrite(pin, !digitalRead(pin));</span><br><span class="line">    vTaskDelay(delayTime);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line"> <span class="comment">//定义了两个LEDFLASH类型的结构体变量led1和led2。</span></span><br><span class="line">LEDFLASH led1, led2;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">在setup函数中，通过Serial.begin函数初始化串口，并将led1和led2的引脚号和延迟时间进行初始化。</span></span><br><span class="line"><span class="comment">接着，通过xTaskCreate函数创建两个任务，即led1和led2的闪烁任务。每个任务的参数都是ledFlash函数的指针，该函数用于控制LED灯的闪烁。</span></span><br><span class="line"><span class="comment">同时，每个任务还将led1或led2的地址作为参数传递给ledFlash函数，以便在函数内部使用LED的pin和delayTime变量。如果任务创建成功，将会打印一条提示信息。</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">setup</span><span class="params">()</span> &#123;</span><br><span class="line">  Serial.begin(<span class="number">9600</span>);</span><br><span class="line"></span><br><span class="line"> </span><br><span class="line">  led1.pin = <span class="number">10</span>;</span><br><span class="line">  led1.delayTime = <span class="number">1000</span>;</span><br><span class="line">  led2.pin = <span class="number">11</span>;</span><br><span class="line">  led2.delayTime = <span class="number">3000</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> (xTaskCreate(ledFlash,</span><br><span class="line">                  <span class="string">&quot;FLASH LED&quot;</span>,</span><br><span class="line">                  <span class="number">1024</span>,</span><br><span class="line">                  (<span class="type">void</span> *)&amp;led1,</span><br><span class="line">                  <span class="number">1</span>,</span><br><span class="line">                  <span class="literal">NULL</span>) == pdPASS)</span><br><span class="line">    Serial.println(<span class="string">&quot;led1 flash task Created.&quot;</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> (xTaskCreate(ledFlash,</span><br><span class="line">                  <span class="string">&quot;FLASH LED&quot;</span>,</span><br><span class="line">                  <span class="number">1024</span>,</span><br><span class="line">                  (<span class="type">void</span> *)&amp;led2,</span><br><span class="line">                  <span class="number">1</span>,</span><br><span class="line">                  <span class="literal">NULL</span>) == pdPASS)</span><br><span class="line">    Serial.println(<span class="string">&quot;led2 flash task Created.&quot;</span>);</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">loop</span><span class="params">()</span> &#123;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="互斥（mutex-exclution）"><a href="#互斥（mutex-exclution）" class="headerlink" title="互斥（mutex exclution）"></a>互斥（mutex exclution）</h1><h2 id="未上锁"><a href="#未上锁" class="headerlink" title="未上锁"></a>未上锁</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br></pre></td><td class="code"><pre><span class="line">/*</span><br><span class="line">  程序： 演示 多任务对 共享资源 同时使用的时候，出现的问题</span><br><span class="line">        多渠道销售，超卖的现象  </span><br><span class="line">  公众号：孤独的二进制</span><br><span class="line"></span><br><span class="line">*/</span><br><span class="line"></span><br><span class="line">// 养成良好习惯，被多进程和中断调用的变量使用 volatile 修饰符</span><br><span class="line">volatile uint32_t inventory = 100; //总库存</span><br><span class="line">volatile uint32_t retailCount = 0; //线下销售量</span><br><span class="line">volatile uint32_t onlineCount = 0; //线上销售量</span><br><span class="line"></span><br><span class="line">void retailTask(void *pvParam) &#123;</span><br><span class="line">  while (1) &#123;</span><br><span class="line"></span><br><span class="line">    //以下实现了带有随机延迟的 inventory减1；</span><br><span class="line">    //等效为 inventory--； retailCount++；</span><br><span class="line">    uint32_t inv = inventory;</span><br><span class="line">    for (int i; i &lt; random(10, 100); i++) vTaskDelay(pdMS_TO_TICKS(i));</span><br><span class="line">    if (inventory &gt; 0) &#123;</span><br><span class="line">      inventory = inv - 1;</span><br><span class="line">      retailCount++;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">  &#125;;</span><br><span class="line"></span><br><span class="line">  vTaskDelay(10); //老板要求慢一些，客户升级后，可以再加快速度</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">void onlineTask(void *pvParam) &#123;</span><br><span class="line">  while (1) &#123;</span><br><span class="line"></span><br><span class="line">    //以下实现了带有随机延迟的 inventory减1；</span><br><span class="line">    //等效为 inventory--； retailCount++；</span><br><span class="line">    uint32_t inv = inventory;</span><br><span class="line">    for (int i; i &lt; random(10, 100); i++) vTaskDelay(pdMS_TO_TICKS(i));</span><br><span class="line">    if (inventory &gt; 0) &#123;</span><br><span class="line">      inventory = inv - 1;</span><br><span class="line">      onlineCount++;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    vTaskDelay(10); //老板要求慢一些，客户升级后，可以再加快速度</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">void showTask(void *pvParam) &#123;</span><br><span class="line">  while (1) &#123;</span><br><span class="line"></span><br><span class="line">    printf(&quot;Inventory : %d\n&quot;, inventory);</span><br><span class="line">    printf(&quot;  Retail : %d, Online : %d\n&quot;, retailCount, onlineCount);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    if (inventory == 0 ) &#123;</span><br><span class="line">      uint32_t totalSales = retailCount + onlineCount;</span><br><span class="line">      printf(&quot;-----SALES SUMMARY-----\n&quot;);</span><br><span class="line">      printf(&quot;  Total Sales:  %d\n&quot;, totalSales);</span><br><span class="line">      printf(&quot;  OverSales:  %d\n&quot;, 100 - totalSales);</span><br><span class="line">    &#125;</span><br><span class="line">    vTaskDelay(pdMS_TO_TICKS(100));</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">void setup() &#123;</span><br><span class="line">  // put your setup code here, to run once:</span><br><span class="line">  Serial.begin(115200);</span><br><span class="line"></span><br><span class="line">  xTaskCreate(onlineTask,</span><br><span class="line">              &quot;Online Channel&quot;,</span><br><span class="line">              1024 * 4,</span><br><span class="line">              NULL,</span><br><span class="line">              1,</span><br><span class="line">              NULL);</span><br><span class="line"></span><br><span class="line">  xTaskCreate(retailTask,</span><br><span class="line">              &quot;Retail Channel&quot;,</span><br><span class="line">              1024 * 4,</span><br><span class="line">              NULL,</span><br><span class="line">              1,</span><br><span class="line">              NULL);</span><br><span class="line"></span><br><span class="line">  xTaskCreate(showTask,</span><br><span class="line">              &quot;Display Inventory&quot;,</span><br><span class="line">              1024 * 4,</span><br><span class="line">              NULL,</span><br><span class="line">              1,</span><br><span class="line">              NULL);</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">void loop() &#123;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="上锁"><a href="#上锁" class="headerlink" title="上锁"></a>上锁</h2><blockquote>
<p>这段代码是一个在多任务环境下实现资源共享并保护共享资源的示例。它使用了 FreeRTOS 的 Semaphore 模块来创建互斥量 Mutex，并用于保护 inventory 变量。其中 retailTask 和 onlineTask 是两个任务，它们会修改共享变量 inventory 的值。showTask 任务会定期输出 inventory、retailCount 和 onlineCount 的值。</p>
<p>这个示例利用了 Mutex 互斥量的原理来保护共享资源。Mutex 是一种二进制信号量，它只能被一个任务持有。当任务需要访问共享资源时，它需要先获得 Mutex 互斥量。如果 Mutex 正被其他任务持有，则当前任务会被阻塞，直到 Mutex 被释放。当任务访问完成后，它需要释放 Mutex 互斥量，以供其他任务访问。</p>
<p>值得注意的是，共享变量被定义为 volatile 类型，以确保在多任务和中断环境下对共享变量的访问是安全和正确的。同时在取得Mutex后，对共享资源进行修改的这段代码叫做 Critical Section。由于 Critical Section 在同一时刻只能被一个任务执行，因此要尽可能快地完成其中的操作，以尽快释放 Mutex 互斥量，以供其他任务访问。</p>
<p>另外，代码中的 vTaskDelay() 会使当前任务暂停一段时间，以释放 CPU 资源，让其他任务能够运行。这样可以确保在多任务环境下，每个任务都有充足的 CPU 时间，以避免出现饥饿现象。</p>
<p>最后，如果 xSemaphoreCreateMutex() 返回 NULL，说明系统没有足够的内存来创建 Mutex 互斥量。需要在系统运行时动态调整堆内存的大小，以确保能够创建 Mutex 互斥量。</p>
</blockquote>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">  程序： Tasks之间数据传递</span></span><br><span class="line"><span class="comment">        有多任务同时写入，或者数据大小超过cpu内存通道时，或者对共享资源的访问时候，需要有防范机制</span></span><br><span class="line"><span class="comment">        使用MUTEX对数据对Cirtical Section的内容进行保护</span></span><br><span class="line"><span class="comment">        可以想象成MUTEX就是一把锁</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">  公众号：孤独的二进制</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">  语法：</span></span><br><span class="line"><span class="comment">  SemaphoreHandle_t xHandler; 创建Handler</span></span><br><span class="line"><span class="comment">  xHandler = xSemaphoreCreateMutex(); 创建一个MUTEX 返回NULL，或者handler</span></span><br><span class="line"><span class="comment">  xSemaphoreGive(xHandler); 释放</span></span><br><span class="line"><span class="comment">  xSemaphoreTake(xHanlder, timeout); 指定时间内获取信号量 返回pdPASS, 或者pdFAIL</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">  理解方法：</span></span><br><span class="line"><span class="comment">  MUTEX的工作原理可以想象成</span></span><br><span class="line"><span class="comment">  共享的资源被锁在了一个箱子里，只有一把钥匙，有钥匙的任务才能对改资源进行访问</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 养成良好习惯，被多进程和中断调用的变量使用 volatile 修饰符</span></span><br><span class="line"><span class="keyword">volatile</span> <span class="type">uint32_t</span> inventory = <span class="number">100</span>; <span class="comment">//总库存</span></span><br><span class="line"><span class="keyword">volatile</span> <span class="type">uint32_t</span> retailCount = <span class="number">0</span>; <span class="comment">//线下销售量</span></span><br><span class="line"><span class="keyword">volatile</span> <span class="type">uint32_t</span> onlineCount = <span class="number">0</span>; <span class="comment">//线上销售量</span></span><br><span class="line"></span><br><span class="line">SemaphoreHandle_t xMutexInventory = <span class="literal">NULL</span>; <span class="comment">//创建信号量Handler</span></span><br><span class="line"></span><br><span class="line">TickType_t timeOut = <span class="number">1000</span>; <span class="comment">//用于获取信号量的Timeout 1000 ticks</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">retailTask</span><span class="params">(<span class="type">void</span> *pvParam)</span> </span>&#123;</span><br><span class="line">  <span class="keyword">while</span> (<span class="number">1</span>) &#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 在timeout的时间内如果能够获取就继续</span></span><br><span class="line">    <span class="comment">// 通俗一些：获取钥匙</span></span><br><span class="line">    <span class="keyword">if</span> (<span class="built_in">xSemaphoreTake</span>(xMutexInventory, timeOut) == pdPASS) &#123;</span><br><span class="line">      <span class="comment">//被MUTEX保护的内容叫做 Critical Section</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">      <span class="comment">//以下实现了带有随机延迟的 inventory减1；</span></span><br><span class="line">      <span class="comment">//等效为 inventory--； retailCount++；</span></span><br><span class="line">      <span class="type">uint32_t</span> inv = inventory;</span><br><span class="line">      <span class="keyword">for</span> (<span class="type">int</span> i; i &lt; <span class="built_in">random</span>(<span class="number">10</span>, <span class="number">100</span>); i++) <span class="built_in">vTaskDelay</span>(<span class="built_in">pdMS_TO_TICKS</span>(i));</span><br><span class="line">      <span class="keyword">if</span> (inventory &gt; <span class="number">0</span>) &#123;</span><br><span class="line">        inventory = inv - <span class="number">1</span>;</span><br><span class="line">        retailCount++;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//释放钥匙</span></span><br><span class="line">        <span class="built_in">xSemaphoreGive</span>(xMutexInventory);</span><br><span class="line">      &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="comment">//无法获取钥匙</span></span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    &#125;;</span><br><span class="line"></span><br><span class="line">    <span class="built_in">vTaskDelay</span>(<span class="number">100</span>); <span class="comment">//老板要求慢一些，客户升级后，可以再加快速度</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">onlineTask</span><span class="params">(<span class="type">void</span> *pvParam)</span> </span>&#123;</span><br><span class="line">  <span class="keyword">while</span> (<span class="number">1</span>) &#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 在timeout的时间内如果能够获取二进制信号量就继续</span></span><br><span class="line">    <span class="comment">// 通俗一些：获取钥匙</span></span><br><span class="line">    <span class="keyword">if</span> (<span class="built_in">xSemaphoreTake</span>(xMutexInventory, timeOut) == pdPASS) &#123;</span><br><span class="line">      <span class="comment">//被MUTEX保护的内容叫做 Critical Section</span></span><br><span class="line">      <span class="comment">//以下实现了带有随机延迟的 inventory减1；</span></span><br><span class="line">      <span class="comment">//等效为 inventory--； retailCount++；</span></span><br><span class="line">      <span class="type">uint32_t</span> inv = inventory;</span><br><span class="line">      <span class="keyword">for</span> (<span class="type">int</span> i; i &lt; <span class="built_in">random</span>(<span class="number">10</span>, <span class="number">100</span>); i++) <span class="built_in">vTaskDelay</span>(<span class="built_in">pdMS_TO_TICKS</span>(i));</span><br><span class="line">      <span class="keyword">if</span> (inventory &gt; <span class="number">0</span>) &#123;</span><br><span class="line">        inventory = inv - <span class="number">1</span>;</span><br><span class="line">        onlineCount++;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//释放钥匙</span></span><br><span class="line">        <span class="built_in">xSemaphoreGive</span>(xMutexInventory);</span><br><span class="line">      &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="comment">//无法获取钥匙</span></span><br><span class="line">      &#125;</span><br><span class="line">    &#125;;</span><br><span class="line"></span><br><span class="line">    <span class="built_in">vTaskDelay</span>(<span class="number">100</span>); <span class="comment">//老板要求慢一些，客户升级后，可以再加快速度</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">showTask</span><span class="params">(<span class="type">void</span> *pvParam)</span> </span>&#123;</span><br><span class="line">  <span class="keyword">while</span> (<span class="number">1</span>) &#123;</span><br><span class="line"></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;Inventory : %d\n&quot;</span>, inventory);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;  Retail : %d, Online : %d\n&quot;</span>, retailCount, onlineCount);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (inventory == <span class="number">0</span> ) &#123;</span><br><span class="line">      <span class="type">uint32_t</span> totalSales = retailCount + onlineCount;</span><br><span class="line">      <span class="built_in">printf</span>(<span class="string">&quot;-----SALES SUMMARY-----\n&quot;</span>);</span><br><span class="line">      <span class="built_in">printf</span>(<span class="string">&quot;  Total Sales:  %d\n&quot;</span>, totalSales);</span><br><span class="line">      <span class="built_in">printf</span>(<span class="string">&quot;  OverSales:  %d\n&quot;</span>, <span class="number">100</span> - totalSales);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">vTaskDelay</span>(<span class="built_in">pdMS_TO_TICKS</span>(<span class="number">1000</span>));</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">setup</span><span class="params">()</span> </span>&#123;</span><br><span class="line">  <span class="comment">// put your setup code here, to run once:</span></span><br><span class="line">  Serial.<span class="built_in">begin</span>(<span class="number">115200</span>);</span><br><span class="line"></span><br><span class="line">  xMutexInventory = <span class="built_in">xSemaphoreCreateMutex</span>(); <span class="comment">//创建MUTEX</span></span><br><span class="line"></span><br><span class="line">  <span class="keyword">if</span> (xMutexInventory == <span class="literal">NULL</span>) &#123;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;No Enough Ram, Unable to Create Semaphore.&quot;</span>);</span><br><span class="line">  &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    <span class="built_in">xTaskCreate</span>(onlineTask,</span><br><span class="line">                <span class="string">&quot;Online Channel&quot;</span>,</span><br><span class="line">                <span class="number">1024</span> * <span class="number">4</span>,</span><br><span class="line">                <span class="literal">NULL</span>,</span><br><span class="line">                <span class="number">1</span>,</span><br><span class="line">                <span class="literal">NULL</span>);</span><br><span class="line">    <span class="built_in">xTaskCreate</span>(retailTask,</span><br><span class="line">                <span class="string">&quot;Retail Channel&quot;</span>,</span><br><span class="line">                <span class="number">1024</span> * <span class="number">4</span>,</span><br><span class="line">                <span class="literal">NULL</span>,</span><br><span class="line">                <span class="number">1</span>,</span><br><span class="line">                <span class="literal">NULL</span>);</span><br><span class="line">    <span class="built_in">xTaskCreate</span>(showTask,</span><br><span class="line">                <span class="string">&quot;Display Inventory&quot;</span>,</span><br><span class="line">                <span class="number">1024</span> * <span class="number">4</span>,</span><br><span class="line">                <span class="literal">NULL</span>,</span><br><span class="line">                <span class="number">1</span>,</span><br><span class="line">                <span class="literal">NULL</span>);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">loop</span><span class="params">()</span> </span>&#123;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>





<h1 id="mutex"><a href="#mutex" class="headerlink" title="mutex"></a>mutex</h1><blockquote>
<p><a target="_blank" rel="noopener" href="https://wokwi.com/projects/333400924498166356">FREERTOS - MPU6050 MUTEX - Wokwi Arduino and ESP32 Simulator</a></p>
</blockquote>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">   程序：  MPU6050 &amp; MUTEX</span></span><br><span class="line"><span class="comment">   公众号：孤独的二进制</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;Adafruit_MPU6050.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;Adafruit_Sensor.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;Wire.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;LiquidCrystal_I2C.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="function">LiquidCrystal_I2C <span class="title">lcd</span><span class="params">(<span class="number">0x27</span>, <span class="number">20</span>, <span class="number">4</span>)</span></span>;</span><br><span class="line"></span><br><span class="line">Adafruit_MPU6050 mpu;</span><br><span class="line">Adafruit_Sensor *mpu_temp, *mpu_accel, *mpu_gyro;</span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="keyword">struct</span> &#123;</span><br><span class="line">  <span class="type">float</span> temp;</span><br><span class="line">  <span class="type">float</span> accX;</span><br><span class="line">  <span class="type">float</span> accY;</span><br><span class="line">  <span class="type">float</span> accZ;</span><br><span class="line">  <span class="type">float</span> gyroX;</span><br><span class="line">  <span class="type">float</span> gyroY;</span><br><span class="line">  <span class="type">float</span> gyroZ;</span><br><span class="line">&#125; MPU6050;</span><br><span class="line"></span><br><span class="line">MPU6050 mpu6050;</span><br><span class="line"></span><br><span class="line">SemaphoreHandle_t xMutexMPU6050 = <span class="literal">NULL</span>; <span class="comment">//创建信号量Handler</span></span><br><span class="line">TickType_t timeOut = <span class="number">1000</span>; <span class="comment">//用于获取信号量的Timeout 1000 ticks</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">mpu6050Task</span><span class="params">(<span class="type">void</span> *pvParam)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">  mpu.<span class="built_in">begin</span>();</span><br><span class="line"></span><br><span class="line">  mpu_temp = mpu.<span class="built_in">getTemperatureSensor</span>();</span><br><span class="line">  mpu_temp-&gt;<span class="built_in">printSensorDetails</span>();</span><br><span class="line"></span><br><span class="line">  mpu_accel = mpu.<span class="built_in">getAccelerometerSensor</span>();</span><br><span class="line">  mpu_accel-&gt;<span class="built_in">printSensorDetails</span>();</span><br><span class="line"></span><br><span class="line">  mpu_gyro = mpu.<span class="built_in">getGyroSensor</span>();</span><br><span class="line">  mpu_gyro-&gt;<span class="built_in">printSensorDetails</span>();</span><br><span class="line"></span><br><span class="line">  <span class="type">sensors_event_t</span> accel;</span><br><span class="line">  <span class="type">sensors_event_t</span> gyro;</span><br><span class="line">  <span class="type">sensors_event_t</span> temp;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">while</span> (<span class="number">1</span>) &#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (<span class="built_in">xSemaphoreTake</span>(xMutexMPU6050, timeOut) == pdPASS) &#123;</span><br><span class="line"></span><br><span class="line">      <span class="comment">//获取MPU数据</span></span><br><span class="line">      mpu_temp-&gt;<span class="built_in">getEvent</span>(&amp;temp);</span><br><span class="line">      mpu_accel-&gt;<span class="built_in">getEvent</span>(&amp;accel);</span><br><span class="line">      mpu_gyro-&gt;<span class="built_in">getEvent</span>(&amp;gyro);</span><br><span class="line"></span><br><span class="line">      mpu6050.temp = temp.temperature;</span><br><span class="line">      mpu6050.accX = accel.acceleration.x;</span><br><span class="line">      mpu6050.accY = accel.acceleration.y;</span><br><span class="line">      mpu6050.accZ = accel.acceleration.z;</span><br><span class="line">      mpu6050.gyroX = gyro.gyro.x;</span><br><span class="line">      mpu6050.gyroY = gyro.gyro.y;</span><br><span class="line">      mpu6050.gyroZ = gyro.gyro.z;</span><br><span class="line"></span><br><span class="line">      <span class="built_in">xSemaphoreGive</span>(xMutexMPU6050); <span class="comment">//释放钥匙</span></span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      <span class="comment">//Unable to obtain MUTEX</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="built_in">vTaskDelay</span>(<span class="number">500</span>);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">lcdTask</span><span class="params">(<span class="type">void</span> *ptParam)</span> </span>&#123;  <span class="comment">//LCD任务主体</span></span><br><span class="line"></span><br><span class="line">  lcd.<span class="built_in">init</span>();</span><br><span class="line">  lcd.<span class="built_in">backlight</span>();</span><br><span class="line"></span><br><span class="line">  <span class="comment">//定义是 2004 LCD</span></span><br><span class="line">  byte lcdLine = <span class="number">4</span>;</span><br><span class="line">  byte lcdChar = <span class="number">20</span>;</span><br><span class="line"></span><br><span class="line">  <span class="comment">//创建一个二维的的数组</span></span><br><span class="line">  <span class="comment">//注意长度是 lcdChar+1 最后还有一个位置要给换行符</span></span><br><span class="line">  <span class="type">char</span> line0[lcdChar + <span class="number">1</span>], line1[lcdChar + <span class="number">1</span>], line2[lcdChar + <span class="number">1</span>], line3[lcdChar + <span class="number">1</span>];</span><br><span class="line">  <span class="type">char</span> * line[] = &#123; line0, line1, line2, line3, &#125;;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">while</span> (<span class="number">1</span>) &#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (<span class="built_in">xSemaphoreTake</span>(xMutexMPU6050, timeOut) == pdPASS) &#123;</span><br><span class="line"></span><br><span class="line">      <span class="comment">// 组合数据</span></span><br><span class="line">      <span class="built_in">sprintf</span>(line0, <span class="string">&quot;     MPU6050 %d&quot;</span>, <span class="built_in">xTaskGetTickCount</span>() / <span class="number">100</span>);</span><br><span class="line">      <span class="built_in">sprintf</span>(line1, <span class="string">&quot; Temperature  %.2f&quot;</span>, mpu6050.temp);</span><br><span class="line">      <span class="built_in">sprintf</span>(line2, <span class="string">&quot; ACC  %.2f %.2f %.2f&quot;</span>, mpu6050.accX, mpu6050.accY, mpu6050.accZ);</span><br><span class="line">      <span class="built_in">sprintf</span>(line3, <span class="string">&quot; GYRO %.2f %.2f %.2f&quot;</span>, mpu6050.gyroX, mpu6050.gyroY, mpu6050.gyroZ);</span><br><span class="line"></span><br><span class="line">      <span class="built_in">xSemaphoreGive</span>(xMutexMPU6050); <span class="comment">//释放钥匙</span></span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      <span class="comment">//Unable to obtain MUTEX</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 显示数据</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; <span class="number">4</span>; i++) &#123;</span><br><span class="line">      lcd.<span class="built_in">setCursor</span>(<span class="number">0</span>, i);</span><br><span class="line">      lcd.<span class="built_in">print</span>(line[i]);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="built_in">vTaskDelay</span>(<span class="number">1000</span>);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">setup</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">  Serial.<span class="built_in">begin</span>(<span class="number">115200</span>);</span><br><span class="line"></span><br><span class="line">  xMutexMPU6050 = <span class="built_in">xSemaphoreCreateMutex</span>(); <span class="comment">//创建MUTEX</span></span><br><span class="line"></span><br><span class="line">  <span class="built_in">xTaskCreate</span>(mpu6050Task, <span class="string">&quot;MPU6050&quot;</span>, <span class="number">1024</span> * <span class="number">8</span>, <span class="literal">NULL</span>, <span class="number">1</span>, <span class="literal">NULL</span>);</span><br><span class="line">  <span class="built_in">vTaskDelay</span>(<span class="number">1000</span>); <span class="comment">//让MPU6050提前先运行一秒获取第一笔数据</span></span><br><span class="line">  <span class="built_in">xTaskCreate</span>(lcdTask, <span class="string">&quot;lcd&quot;</span>, <span class="number">1024</span> * <span class="number">8</span>, <span class="literal">NULL</span>, <span class="number">1</span>, <span class="literal">NULL</span>);</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">loop</span><span class="params">()</span> </span>&#123;&#125;</span><br></pre></td></tr></table></figure>

<h1 id="如何创建U8G2-OLED的FREERTOS任务"><a href="#如何创建U8G2-OLED的FREERTOS任务" class="headerlink" title="如何创建U8G2 OLED的FREERTOS任务"></a>如何创建U8G2 OLED的FREERTOS任务</h1><h2 id="非任务"><a href="#非任务" class="headerlink" title="非任务"></a>非任务</h2><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">   程序：  ESP32 OLED 使用 U8G2库</span></span><br><span class="line"><span class="comment">          使用FREERTOS</span></span><br><span class="line"><span class="comment">   公众号：孤独的二进制</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;U8g2lib.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;Wire.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">oledTask</span><span class="params">(<span class="type">void</span> * pvParam)</span> </span>&#123;</span><br><span class="line">  <span class="function">U8G2_SSD1306_128X64_NONAME_F_HW_I2C <span class="title">u8g2</span><span class="params">(U8G2_R0, <span class="comment">/* reset=*/</span> U8X8_PIN_NONE)</span></span>;</span><br><span class="line">  u8g2.<span class="built_in">begin</span>();</span><br><span class="line">  <span class="keyword">for</span> (;;) &#123;</span><br><span class="line">    u8g2.<span class="built_in">clearBuffer</span>();					<span class="comment">// clear the internal memory</span></span><br><span class="line">    u8g2.<span class="built_in">setFont</span>(u8g2_font_ncenB08_tr);	<span class="comment">// choose a suitable font</span></span><br><span class="line">    u8g2.<span class="built_in">drawStr</span>(<span class="number">15</span>, <span class="number">10</span>, <span class="string">&quot;LONELY BINARY&quot;</span>);	<span class="comment">// write something to the internal memory</span></span><br><span class="line">    u8g2.<span class="built_in">sendBuffer</span>();					<span class="comment">// transfer internal memory to the display</span></span><br><span class="line">    <span class="built_in">vTaskDelay</span>(<span class="number">1000</span>);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">setup</span><span class="params">()</span> </span>&#123; <span class="comment">//loopBack , Priority 1, Core 1</span></span><br><span class="line">  <span class="built_in">xTaskCreatePinnedToCore</span>(oledTask, <span class="string">&quot;OLED Task&quot;</span>, <span class="number">1024</span> * <span class="number">6</span>, <span class="literal">NULL</span>, <span class="number">1</span>, <span class="literal">NULL</span>, <span class="number">1</span>);</span><br><span class="line"></span><br><span class="line">  <span class="built_in">vTaskDelete</span>(<span class="literal">NULL</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">loop</span><span class="params">()</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>





<h2 id="任务"><a href="#任务" class="headerlink" title="任务"></a>任务</h2><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">   程序：  ESP32 OLED 使用 U8G2库</span></span><br><span class="line"><span class="comment">          使用FREERTOS</span></span><br><span class="line"><span class="comment">   公众号：孤独的二进制</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;U8g2lib.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;Wire.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">oledTask</span><span class="params">(<span class="type">void</span> * pvParam)</span> </span>&#123;</span><br><span class="line">  <span class="function">U8G2_SSD1306_128X64_NONAME_F_HW_I2C <span class="title">u8g2</span><span class="params">(U8G2_R0, <span class="comment">/* reset=*/</span> U8X8_PIN_NONE)</span></span>;</span><br><span class="line">  u8g2.<span class="built_in">begin</span>();</span><br><span class="line">  <span class="keyword">for</span> (;;) &#123;</span><br><span class="line">    u8g2.<span class="built_in">clearBuffer</span>();					<span class="comment">// clear the internal memory</span></span><br><span class="line">    u8g2.<span class="built_in">setFont</span>(u8g2_font_ncenB08_tr);	<span class="comment">// choose a suitable font</span></span><br><span class="line">    u8g2.<span class="built_in">drawStr</span>(<span class="number">15</span>, <span class="number">10</span>, <span class="string">&quot;LONELY BINARY&quot;</span>);	<span class="comment">// write something to the internal memory</span></span><br><span class="line">    u8g2.<span class="built_in">sendBuffer</span>();					<span class="comment">// transfer internal memory to the display</span></span><br><span class="line">    <span class="built_in">vTaskDelay</span>(<span class="number">1000</span>);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">setup</span><span class="params">()</span> </span>&#123; <span class="comment">//loopBack , Priority 1, Core 1</span></span><br><span class="line">  <span class="built_in">xTaskCreatePinnedToCore</span>(oledTask, <span class="string">&quot;OLED Task&quot;</span>, <span class="number">1024</span> * <span class="number">6</span>, <span class="literal">NULL</span>, <span class="number">1</span>, <span class="literal">NULL</span>, <span class="number">1</span>);</span><br><span class="line"></span><br><span class="line">  <span class="built_in">vTaskDelete</span>(<span class="literal">NULL</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">loop</span><span class="params">()</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


      
    </div>
    <div class="article-footer">
      <blockquote class="mt-2x">
  <ul class="post-copyright list-unstyled">
    
    <li class="post-copyright-link hidden-xs">
      <strong>本文链接：</strong>
      <a href="https://gitee.com/yin-qiyu/myblog/2023/04/04/FreeRTOS/lonely_binary/" title="" target="_blank" rel="external">https://gitee.com/yin-qiyu/myblog/2023/04/04/FreeRTOS/lonely_binary/</a>
    </li>
    
    <li class="post-copyright-license">
      <strong>版权声明： </strong> 本博客所有文章除特别声明外，均采用 <a href="http://creativecommons.org/licenses/by/4.0/deed.zh" target="_blank" rel="external">CC BY 4.0 CN协议</a> 许可协议。转载请注明出处！
    </li>
  </ul>
</blockquote>


<div class="panel panel-default panel-badger">
  <div class="panel-body">
    <figure class="media">
      <div class="media-left">
        <a href="https://github.com/cofess" target="_blank" class="img-burn thumb-sm visible-lg">
          <img src="/yin-qiyu/myblog/images/avatar.jpg" class="img-rounded w-full" alt="">
        </a>
      </div>
      <div class="media-body">
        <h3 class="media-heading"><a href="https://github.com/cofess" target="_blank"><span class="text-dark">昵称</span><small class="ml-1x">Web Developer &amp; Designer</small></a></h3>
        <div>个人简介。</div>
      </div>
    </figure>
  </div>
</div>


    </div>
  </article>
  
    
  <section id="comments">
  	
      <div id="vcomments"></div>
    
  </section>


  
</div>

  <nav class="bar bar-footer clearfix" data-stick-bottom>
  <div class="bar-inner">
  
  <ul class="pager pull-left">
    
    <li class="prev">
      <a href="/yin-qiyu/myblog/2023/07/13/hello-world/" title="Hello World"><i class="icon icon-angle-left" aria-hidden="true"></i><span>&nbsp;&nbsp;Newer</span></a>
    </li>
    
    
    <li class="next">
      <a href="/yin-qiyu/myblog/2023/03/16/esp/esp-idf/issue/" title="(no title)"><span>Older&nbsp;&nbsp;</span><i class="icon icon-angle-right" aria-hidden="true"></i></a>
    </li>
    
    
  </ul>
  
  
  <!-- Button trigger modal -->
  <button type="button" class="btn btn-fancy btn-donate pop-onhover bg-gradient-warning" data-toggle="modal" data-target="#donateModal"><span>$</span></button>
  <!-- <div class="wave-icon wave-icon-danger btn-donate" data-toggle="modal" data-target="#donateModal">
    <div class="wave-circle"><span class="icon"><i class="icon icon-bill"></i></span></div>
  </div> -->
  
  
  <div class="bar-right">
    
    <div class="share-component" data-sites="weibo,qq,wechat,facebook,twitter" data-mobile-sites="weibo,qq,qzone"></div>
    
  </div>
  </div>
</nav>
  
<!-- Modal -->
<div class="modal modal-center modal-small modal-xs-full fade" id="donateModal" tabindex="-1" role="dialog">
  <div class="modal-dialog" role="document">
    <div class="modal-content donate">
      <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
      <div class="modal-body">
        <div class="donate-box">
          <div class="donate-head">
            <p>Maybe you could buy me a cup of coffee.</p>
          </div>
          <div class="tab-content">
            <div role="tabpanel" class="tab-pane fade active in" id="alipay">
              <div class="donate-payimg">
                <img src="/yin-qiyu/myblog/images/donate/alipayimg.png" alt="Scan Qrcode" title="Scan" />
              </div>
              <p class="text-muted mv">Scan this qrcode</p>
              <p class="text-grey">Open alipay app scan this qrcode, buy me a coffee!</p>
            </div>
            <div role="tabpanel" class="tab-pane fade" id="wechatpay">
              <div class="donate-payimg">
                <img src="/yin-qiyu/myblog/images/donate/wechatpayimg.png" alt="Scan Qrcode" title="Scan" />
              </div>
              <p class="text-muted mv">Scan this qrcode</p>
              <p class="text-grey">Open wechat app scan this qrcode, buy me a coffee!</p>
            </div>
          </div>
          <div class="donate-footer">
            <ul class="nav nav-tabs nav-justified" role="tablist">
              <li role="presentation" class="active">
                <a href="#alipay" id="alipay-tab" role="tab" data-toggle="tab" aria-controls="alipay" aria-expanded="true"><i class="icon icon-alipay"></i> alipay</a>
              </li>
              <li role="presentation" class="">
                <a href="#wechatpay" role="tab" id="wechatpay-tab" data-toggle="tab" aria-controls="wechatpay" aria-expanded="false"><i class="icon icon-wepay"></i> wechat payment</a>
              </li>
            </ul>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>



</main>

  <footer class="footer" itemscope itemtype="http://schema.org/WPFooter">
	
	
    <ul class="social-links">
    	
        <li><a href="https://github.com/cofess" target="_blank" title="Github" data-toggle=tooltip data-placement=top><i class="icon icon-github"></i></a></li>
        
        <li><a href="http://weibo.com/cofess" target="_blank" title="Weibo" data-toggle=tooltip data-placement=top><i class="icon icon-weibo"></i></a></li>
        
        <li><a href="https://twitter.com/iwebued" target="_blank" title="Twitter" data-toggle=tooltip data-placement=top><i class="icon icon-twitter"></i></a></li>
        
        <li><a href="https://www.behance.net/cofess" target="_blank" title="Behance" data-toggle=tooltip data-placement=top><i class="icon icon-behance"></i></a></li>
        
        <li><a href="/yin-qiyu/myblog/atom.xml" target="_blank" title="Rss" data-toggle=tooltip data-placement=top><i class="icon icon-rss"></i></a></li>
        
    </ul>

    <div class="copyright">
    	
        <div class="publishby">
        	Theme by <a href="https://github.com/cofess" target="_blank"> cofess </a>base on <a href="https://github.com/cofess/hexo-theme-pure" target="_blank">pure</a>.
        </div>
    </div>
</footer>
  <script src="//cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
<script>
window.jQuery || document.write('<script src="js/jquery.min.js"><\/script>')
</script>

<script src="/yin-qiyu/myblog/js/plugin.min.js"></script>


<script src="/yin-qiyu/myblog/js/application.js"></script>


    <script>
(function (window) {
    var INSIGHT_CONFIG = {
        TRANSLATION: {
            POSTS: 'Posts',
            PAGES: 'Pages',
            CATEGORIES: 'Categories',
            TAGS: 'Tags',
            UNTITLED: '(Untitled)',
        },
        ROOT_URL: '/yin-qiyu/myblog/',
        CONTENT_URL: '/yin-qiyu/myblog/content.json',
    };
    window.INSIGHT_CONFIG = INSIGHT_CONFIG;
})(window);
</script>

<script src="/yin-qiyu/myblog/js/insight.js"></script>






   




   
    
  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/valine"></script>
  <script type="text/javascript">
  var GUEST = ['nick', 'mail', 'link'];
  var meta = 'nick,mail,link';
  meta = meta.split(',').filter(function(item) {
    return GUEST.indexOf(item) > -1;
  });
  new Valine({
    el: '#vcomments',
    verify: false,
    notify: false,
    appId: '',
    appKey: '',
    placeholder: 'Just go go',
    avatar: 'mm',
    meta: meta,
    pageSize: '10' || 10,
    visitor: false
  });
  </script>

     







</body>
</html>